/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.dialog;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import tnviet.proj.jna.JNCaptor;
import tnviet.proj.jna.Program;
import tnviet.proj.jna.utilities.ResourceManager;

/**
 *
 * @author Administrator
 */
public class JNCaptureDialog extends JDialog implements ActionListener {

    JpcapCaptor jpcap = null;
    NetworkInterface[] devices;
    JNCaptor captor;

    JComboBox devicesComboBox, timeStopComboBox;
    JRadioButton wholeCheck, headCheck, userCheck;
    JCheckBox promisCheck, packetNumberStopCheck, timeStopCheck;
    JFormattedTextField  timeStopField, packetNumberStopField;
    JTextField filterField;
    JPanel devicesPane, stopPane, stopPane1, stopPane2, stopPane3, buttonPane, westPane, optionsPane;
    JLabel label1, label2, filterLabel;
    JButton okButton, cancelButton;
    long alarmPacketsCount = 0, alarmTimeCount = 0;
    int option;
    JFrame owner;

    private ResourceManager resourceManager = new ResourceManager(this.getClass(), Program.locale);

    
    public JNCaptureDialog(JFrame parent) {

        //super(parent);
        this.setModal(true);
        this.owner = parent;

        captor = JNCaptor.getInstance();
        
        createDevicePane();
        createPromisCheck();
        createButtonbar();
        createStopAfterPane();
        //Button panel
        westPane = new JPanel();
        westPane.setLayout(new FormLayout("pref", "pref, pref, pref, pref"));
        CellConstraints cc = new CellConstraints();
        westPane.add(devicesPane, cc.xy(1, 1));
        westPane.add(optionsPane, cc.xy(1, 2));
        westPane.add(stopPane, cc.xy(1, 3));
        westPane.add(buttonPane, cc.xy(1, 4));

        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.X_AXIS));
        this.getContentPane().add(Box.createRigidArea(new Dimension(10,10)));
        this.getContentPane().add(westPane);
        this.getContentPane().add(Box.createRigidArea(new Dimension(10,10)));
        this.setResizable(false);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        this.setTitle(resourceManager.getString("captureDialog.title"));
        this.getRootPane().setDefaultButton(okButton);
        this.pack();
        this.setLocationRelativeTo(parent);

    }
    public void createDevicePane(){
        devices = JpcapCaptor.getDeviceList();
        if (devices == null) {
            JOptionPane.showMessageDialog(this,
                    resourceManager.getString("captureDialog.missingDeviceMessage"),
                    resourceManager.getString("captureDialog.error"), JOptionPane.WARNING_MESSAGE);
            dispose();
            return;
        } else {
            String[] names = new String[devices.length];
            for (int i = 0; i < names.length; i++) {
                names[i]=(devices[i].description==null?devices[i].name:devices[i].description);
            }
            devicesComboBox = new JComboBox(names);
        }
        FormLayout layout = new FormLayout("2dlu,pref, 2dlu", "pref");
        CellConstraints cc = new CellConstraints();
        PanelBuilder builder = new PanelBuilder(layout);
        builder.add(devicesComboBox, cc.xy(2, 1));
        builder.setBorder(BorderFactory.createTitledBorder(resourceManager.getString("captureDialog.choosePane")));
        devicesPane = builder.getPanel();
        
    }
    public void createPromisCheck(){
        promisCheck = new JCheckBox(resourceManager.getString("captureDialog.promiscCheck"));
        promisCheck.setSelected(true);
        filterLabel = new JLabel(resourceManager.getString("captureDialog.filter"));
        filterField = new JTextField();
        FormLayout layout = new FormLayout("2dlu,left:pref, 35dlu,pref,10dlu,pref:grow, 2dlu", "pref");
        PanelBuilder builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();
        builder.add(promisCheck, cc.xy(2, 1));
        builder.add(filterLabel, cc.xy(4, 1));
        builder.add(filterField, cc.xy(6, 1));
        builder.setBorder(new TitledBorder(resourceManager.getString("captureDialog.options")));
        optionsPane = builder.getPanel();
        
    }
    public void createButtonbar(){

        okButton = new JButton(resourceManager.getString("captureDialog.okButton"));
        okButton.setActionCommand("OK");
        okButton.addActionListener(this);
        cancelButton = new JButton(resourceManager.getString("captureDialog.cancelButton"));
        cancelButton.setActionCommand("Cancel");
        cancelButton.addActionListener(this);
        buttonPane  = ButtonBarFactory.buildRightAlignedBar(okButton, cancelButton);
        buttonPane.setBorder(BorderFactory.createEmptyBorder(6,6,6,6));
    }
    public void createStopAfterPane(){
        packetNumberStopField = new JFormattedTextField(NumberFormat.getIntegerInstance());
        packetNumberStopField.setPreferredSize(filterField.getPreferredSize());
        timeStopField = new JFormattedTextField(NumberFormat.getIntegerInstance());
        timeStopField.setPreferredSize(filterField.getPreferredSize());
        
        FormLayout layout = new FormLayout("2dlu,50dlu,max(pref;120dlu),35dlu, pref, 2dlu, ", "pref, pref");
        PanelBuilder builder = new PanelBuilder(layout);
        CellConstraints cc = new CellConstraints();
        packetNumberStopCheck = new JCheckBox(resourceManager.getString("captureDialog.after"));
        packetNumberStopCheck.setSelected(false);
        packetNumberStopCheck.setActionCommand("StopByPackets");
        packetNumberStopCheck.addActionListener(this);
        timeStopCheck = new JCheckBox(resourceManager.getString("captureDialog.after"));
        timeStopCheck.setSelected(false);
        timeStopCheck.setActionCommand("StopByTime");
        timeStopCheck.addActionListener(this);
        packetNumberStopField.setEnabled(false);
        timeStopField.setEnabled(false);
        label2 = new JLabel();
        timeStopComboBox = new JComboBox(new String[] {
            resourceManager.getString("captureDialog.minutes"), resourceManager.getString("captureDialog.hours")});
        timeStopComboBox.setEditable(false);
        timeStopComboBox.setEnabled(false);
        label2.setPreferredSize(timeStopComboBox.getPreferredSize());
        label2.setText("Packets");
        builder.add(packetNumberStopCheck, cc.xy(2, 1));
        builder.add(timeStopCheck, cc.xy(2, 2));
        builder.add(packetNumberStopField, cc.xy(3, 1));
        builder.add(timeStopField, cc.xy(3, 2));
        builder.add(label2, cc.xy(5, 1));
        builder.add(timeStopComboBox, cc.xy(5, 2));
        builder.setBorder(BorderFactory.createTitledBorder(resourceManager.getString("captureDialog.stop")));
        stopPane = builder.getPanel();
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if(cmd.equals("StopByPackets")) {
            if (packetNumberStopCheck.isSelected() == true) {
                packetNumberStopField.setEnabled(true);
            } else packetNumberStopField.setEnabled(false);
        } else if(cmd.equals("StopByTime")) {
            if (timeStopCheck.isSelected() == true) {
                timeStopField.setEnabled(true);
                timeStopComboBox.setEnabled(true);
            } else {
                timeStopField.setEnabled(false);
                timeStopComboBox.setEnabled(false);
            }
        } else if(cmd.equals("OK")){
            try {
                jpcap = JpcapCaptor.openDevice(devices[devicesComboBox.getSelectedIndex()], 1514, promisCheck.isSelected(), 50);
                if(filterField.getText()!=null&&filterField.getText().length()>0){
                    jpcap.setFilter(filterField.getText(), true);
                }
                try {
                    timeStopField.commitEdit();
                    packetNumberStopField.commitEdit();
                } catch (ParseException ex) {
                }

                option = JOptionPane.OK_OPTION;
                if((packetNumberStopCheck.isSelected() == true) && (packetNumberStopField.getText()!=null)){
                    //alarmPacketsCount = Integer.valueOf(packetNumberStopField.getValue());
                    alarmPacketsCount = (Long)packetNumberStopField.getValue();
                }
                if(timeStopCheck.isSelected() == true && timeStopCheck.getText()!=null){
                    alarmTimeCount = (Long)timeStopField.getValue();
                    if(timeStopComboBox.getSelectedIndex() == 0){
                        alarmTimeCount = alarmTimeCount * 60000;
                    } else if(timeStopComboBox.getSelectedIndex() == 1){
                        alarmTimeCount = alarmTimeCount * 60000 * 60;
                    }
                }
                setVisible(false);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this,
                        resourceManager.getString("captureDialog.numberException"),
                        resourceManager.getString("captureDialog.error"), JOptionPane.ERROR_MESSAGE);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, resourceManager.getString("captureDialog.filterSyntaxError"),
                        resourceManager.getString("captureDialog.warning"), JOptionPane.WARNING_MESSAGE);
                jpcap = null;
            }
        } else if(cmd.equals("Cancel")) {
            option = JOptionPane.CANCEL_OPTION;
            setVisible(false);
        }
		
    }

    public JpcapCaptor getJpcap() {
        return this.jpcap;
    }
    public long getAlarmPacketsCount(){
        return alarmPacketsCount;
    }
    public long getAlarmTimeCount(){
        return alarmTimeCount;
    }


    public static void main(String[] args){
        JNCaptureDialog a = new JNCaptureDialog(null);
        a.setVisible(true);
    }

    public int getOption(){
        return option;
    }






}
